﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using Intorise.Common;
using System.Net;

namespace Model
{
    public class Command
    {
        public string Name { get; set; }
        public byte[] Data { get; set; }
    }

    public interface IAttactDataPack
    {
        byte[] GetData(Map map, Player player, Monster monster);

        byte[] GetData(Map map, Player player, List<Monster> monsters);
    }

    /// <summary>
    /// 黑龙波
    /// </summary>
    public class HLB : Skill, IAttactDataPack
    {
        public HLB()
        {
            ID = 0x0007A9BB00002720;
            Name = "黑龙波攻击者";
        }

        public byte[] GetData(Map map, Player player, Monster monster)
        {
            var data = new byte[] { 0x00, 0x07, 0xA9, 0xBB, 0x00, 0x00, 0x27, 0x20, 0x00, 0x00, 0x00, 0x03 };
            data = Tools.Contact(data, map.Data,
                            DataPackager.GetNetworkData(player.Line), DataPackager.GetPointData(player.X, player.Y), new byte[] { 0x00, 0x01 },
                            DataPackager.GetNetworkData(monster.InstanceID), new byte[] { 0x00, 0x01, 0x02 });
            return data = Tools.Contact(DataPackager.GetNetworkData(data.Length), data);
        }


        public byte[] GetData(Map map, Player player, List<Monster> monsters)
        {
            var data = new byte[] { 0x00, 0x07, 0xA9, 0xBB, 0x00, 0x00, 0x27, 0x20, 0x00, 0x00, 0x00, 0x05 };
            data = Tools.Contact(data, map.Data,
                             DataPackager.GetNetworkData(player.Line), DataPackager.GetPointData(player.X, player.Y), DataPackager.GetNetworkData((short)monsters.Count));
            monsters.ForEach(w =>
            {
                data = Tools.Contact(data, DataPackager.GetNetworkData(w.InstanceID));
            });
            data = Tools.Contact(data, DataPackager.GetNetworkData((short)monsters.Count));
            monsters.ForEach(w =>
            {
                data = Tools.Contact(data, new byte[] { 0x02 });
            });
            data = Tools.Contact(DataPackager.GetNetworkData(data.Length), data);
            return data;
        }
    }

    /// <summary>
    /// 陨石术
    /// </summary>
    public class YSS : Skill, IAttactDataPack
    {
        public YSS()
        {
            ID = 0x0007A9BB00002720;
            Name = "陨石术攻击者";
        }

        public byte[] GetData(Map map, Player player, Monster monster)
        {
            return Tools.Contact(new byte[] { 0x00, 0x00, 0x00, 0x12, 0x00, 0x01, 0x8F, 0x39, 0xb8, 0x9c, 0x01, 0x08 },player.Version, DataPackager.GetNetworkData(monster.InstanceID));
            //return Tools.Contact(new byte[] { 0x00, 0x00, 0x00, 0x14, 0x00, 0x01, 0x8F, 0x39, 0x00, 0x00, 0x27, 0x1C, 0x00, 0x00, 0x00, 0x07 }, DataPackager.GetNetworkData(monster.InstanceID));
        }


        public byte[] GetData(Map map, Player player, List<Monster> monsters)
        {
            throw new NotImplementedException();
        }
    }

    /// <summary>
    /// 霹雳回旋斩
    /// </summary>
    public class PLHXZ : Skill, IAttactDataPack
    {
        public PLHXZ()
        {
            ID = 0x00018F3900002717;
            Name = "霹雳回旋斩攻击者";
        }

        public byte[] GetData(Map map, Player player, Monster monster)
        {
            return Tools.Contact(new byte[] { 0x00, 0x00, 0x00, 0x14, 0x00, 0x01, 0x8f, 0x39, 0x00, 0x00, 0x27, 0x17, 0x00, 0x00, 0x00, 0x01 }, DataPackager.GetNetworkData(monster.InstanceID));
        }


        public byte[] GetData(Map map, Player player, List<Monster> monsters)
        {
            var data = new byte[] { 0x00, 0x01, 0x8f, 0x39, 0x00, 0x00, 0x27, 0x17 };
            //data = Tools.Contact(data, DataPackager.GetNetworkData(monsters.Count), DataPackager.GetNetworkData(monsters.First().InstanceID));
            data = Tools.Contact(data, DataPackager.GetNetworkData(6), DataPackager.GetNetworkData(monsters.First().InstanceID));
            data = Tools.Contact(DataPackager.GetNetworkData(data.Length), data);
            return data;
        }
    }

    /// <summary>
    /// 霹雳地裂斩
    /// </summary>
    public class PLDLZ : Skill, IAttactDataPack
    {
        public PLDLZ()
        {
            ID = 0x00018F3900002719;
            Name = "霹雳地裂斩攻击者";
        }

        public byte[] GetData(Map map, Player player, Monster monster)
        {
            return Tools.Contact(new byte[] { 0x00, 0x00, 0x00, 0x14, 0x00, 0x01, 0x8f, 0x39, 0x00, 0x00, 0x27, 0x19, 0x00, 0x00, 0x00, 0x00 }, DataPackager.GetNetworkData(monster.InstanceID));
        }


        public byte[] GetData(Map map, Player player, List<Monster> monsters)
        {

            var data = new byte[] { 0x00, 0x01, 0x8f, 0x39, 0x00, 0x00, 0x27, 0x19 };
            data = Tools.Contact(data, DataPackager.GetNetworkData(monsters.Count), DataPackager.GetNetworkData(monsters.First().InstanceID));
            data = Tools.Contact(DataPackager.GetNetworkData(data.Length), data);
            return data;
        }
    }

    /// <summary>
    /// 地狱火
    /// </summary>
    public class DYH : Skill, IAttactDataPack
    {
        public DYH()
        {
            ID = 0x00018F3900002722;
            Name = "地狱火攻击者";
        }

        public byte[] GetData(Map map, Player player, Monster monster)
        {
            return Tools.Contact(new byte[] { 0x00, 0x00, 0x00, 0x14, 0x00, 0x01, 0x8f, 0x39, 0x00, 0x00, 0x27, 0x22, 0x00, 0x00, 0x00, 0x01 }, DataPackager.GetNetworkData(monster.InstanceID));
        }


        public byte[] GetData(Map map, Player player, List<Monster> monsters)
        {
            var data = new byte[] { 0x00, 0x01, 0x8f, 0x39, 0x00, 0x00, 0x27, 0x22 };
            data = Tools.Contact(data, DataPackager.GetNetworkData(monsters.Count), DataPackager.GetNetworkData(monsters.First().InstanceID));
            data = Tools.Contact(DataPackager.GetNetworkData(data.Length), data);
            return data;
        }
    }

    public class CTJ : Thing, IAttactDataPack
    {
        public CTJ()
        {
            ID = 0xCC9C01;
            Name = "穿透箭";
        }

        public byte[] GetData(Map map, Player player, Monster monster)
        {
            byte dir = 0;
            if (player.Location.X == monster.Location.X)
            {
                if (player.Location.Y < monster.Location.Y)
                {
                    dir = 0x08;//下
                }
                else
                {
                    dir = 0x00;//上
                }
            }
            else if (player.Location.Y == monster.Location.Y)
            {
                if (player.Location.X < monster.Location.X)
                {
                    dir = 0x04;//右
                }
                else
                {
                    dir = 0x0c;//左
                }
            }
            else if (player.Location.X < monster.Location.X && player.Location.Y < monster.Location.Y)
            {
                dir = 0x06;//右下
            }
            else if (player.Location.X < monster.Location.X && player.Location.Y > monster.Location.Y)
            {
                dir = 0x02;//右上
            }
            else if (player.Location.X > monster.Location.X && player.Location.Y > monster.Location.Y)
            {
                dir = 0x0E;//左上
            }
            else if (player.Location.X > monster.Location.X && player.Location.Y < monster.Location.Y)
            {
                dir = 0x0A;//左下
            }

            return Tools.Contact(new byte[] { 0x00, 0x00, 0x00, 0x12, 0x00, 0x01, 0x8f, 0x39, 0xCC, 0x9C, 0x01 }, new byte[] { dir }, DataPackager.GetNetworkData(monster.InstanceID), player.Version);
        }


        public byte[] GetData(Map map, Player player, List<Monster> monsters)
        {
            var data = new byte[] { 0x00, 0x01, 0x8f, 0x39, 0x00, 0x00, 0x27, 0x22 };
            data = Tools.Contact(data, DataPackager.GetNetworkData(monsters.Count), DataPackager.GetNetworkData(monsters.First().InstanceID));
            data = Tools.Contact(DataPackager.GetNetworkData(data.Length), data);
            return data;
        }
    }
}
